//
//  XGL_LiveItemCollectionViewCell.swift
//  XGamesLive
//
//  Created by ios on 2024/5/29.
//

import UIKit

class XGL_LiveItemCollectionViewCell: UICollectionViewCell {
    
    var coverView: UIImageView!
    var itemTitleText: UILabel!
    var userHeadView: UIImageView!
    var userNameText: UILabel!
    var numberText: UILabel!
    
    var itemData: PublishData = PublishData(){
        didSet{
            self.coverView.loadImageWithUrl(self.itemData.data3 ?? "")
            self.itemTitleText.text = self.itemData.data2
            if let infor = itemData.auditUserExt{
                self.userHeadView.loadImageWithUrl(infor.data2)
                self.userNameText.text = infor.data1
            }
            let number = (Double(arc4random_uniform(20)) / 10.0).roundTo(places: 1)
            self.numberText.text = "\(number)k"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension XGL_LiveItemCollectionViewCell{
    private func createXglView(){
        self.coverView = UIImageView().then({ make in
            make.layerCornerRadius = 12.0
            make.clipsToBounds = true
        })
        self.itemTitleText = UILabel().then { make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
        }
        self.userHeadView = UIImageView().then { make in
            make.layerCornerRadius = 4.0
            make.clipsToBounds = true
        }
        self.userNameText = UILabel().then { make in
            make.textColor = UIColor.hexColor("#A2A2A2")
            make.font = UIFont.pingfangSC_Regular(fontSize: 12.0)
        }
        self.numberText = UILabel().then { make in
            make.textColor = UIColor.hexColor("#80FFE6")
            make.font = UIFont.pingfangSC_Regular(fontSize: 12.0)
        }
        let rightIcon = UIImageView(image: imageWithName("img_xgl_live_right_top"))
        let numberIcon = UIImageView(image: imageWithName("img_xgl_live_number"))
        
        self.contentView.addSubviews([self.coverView,self.itemTitleText,self.userHeadView,self.userNameText,self.numberText,rightIcon,numberIcon])
        
        self.coverView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        rightIcon.snp.makeConstraints { make in
            make.right.equalTo(-10.0)
            make.top.equalTo(10.0)
            make.width.height.equalTo(20.0)
        }
        itemTitleText.snp.makeConstraints { make in
            make.left.equalTo(9.0)
            make.bottom.equalTo(userHeadView.snp.top).offset(-8.0)
            make.centerX.equalToSuperview()
        }
        userHeadView.snp.makeConstraints { make in
            make.left.equalTo(9.0)
            make.bottom.equalTo(-9.0)
            make.width.height.equalTo(24.0)
        }
        userNameText.snp.makeConstraints { make in
            make.left.equalTo(userHeadView.snp.right).offset(8.0)
            make.centerY.equalTo(userHeadView)
        }
        numberText.snp.makeConstraints { make in
            make.centerY.equalTo(userHeadView)
            make.right.equalTo(-10.0)
        }
        numberIcon.snp.makeConstraints { make in
            make.centerY.equalTo(userHeadView)
            make.right.equalTo(numberText.snp.left).offset(-4.0)
        }
    }
}
